package com.gyms.activity;

import android.text.InputFilter;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.classic.okhttp.enums.Enum;
import com.classic.okhttp.services.IUserService;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.R;
import com.gyms.base.MyAutoLayoutActivity;
import com.gyms.manager.TitleManager;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import constant.EventConstant;
import utils.StringUtils;
import weight.UiEvent;

/**
 * 信息修改页面 yjl
 */
public class EditInfoActivity extends MyAutoLayoutActivity {
    @BindView(R.id.tv_lable)
    TextView tvLable;
    @BindView(R.id.et_value)
    EditText etValue;
    private String lable;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_edit_info;
    }

    @Override
    protected void initView() {
        createLoadingView("修改中",true);
        lable = getIntent().getStringExtra("lable");
        mTitleManager.setRightText("确定");
        setBack();
        tvLable.setText("" + lable);
        if ("手机号".equals(lable)) {
            etValue.setInputType(InputType.TYPE_CLASS_PHONE);
            InputFilter[] filters = {new InputFilter.LengthFilter(11)};
            etValue.setFilters(filters);
        }
        etValue.setHint("请输入" + lable);
    }

    @Override
    protected void setListener() {
        mTitleManager.getmRightTV().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String value= etValue.getText().toString().trim();
                if(StringUtils.isNull(value)){
                    ToastUtil.showToast(mContext, "请输入");
                    return;
                }
                updateInfo(Enum.EnumUserFieldType.Nickname,value);
            }
        });
    }

    @Override
    protected void setTitleTheme() {
        mTheme= TitleManager.TitleTheme.White;
    }

    @Override
    protected void processLogic() {

    }

    private void updateInfo(Enum.EnumUserFieldType type,final String value){
        loadingView.show();
        //fieldType:1头像,2昵称,3密码设置fieldValue:密码需要传md5加密过的字符串)
        IUserService.modifyUser(this, type, value, false, new ObjectCallback<Object>() {

            @Override
            public void onSuccess(Object response, String message) {
                dialogDis();
                EventBus.getDefault().post(new UiEvent(EventConstant.S_NAME, value));
                finish();
            }

            @Override
            public void onErrorT(int code, Object response, String errorMessage) {
                dialogDis();
                ToastUtil.showToast(mContext, errorMessage);
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                dialogDis();
            }
        });
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        hideKeyBoard(ev);//隐藏软键盘
        return super.dispatchTouchEvent(ev);
    }
}
